import java.util.Arrays;

public class MyStack {
    private int[] elem ;
    private int usedSize ;
    private static final int DEFAULT_CAPACITY = 5 ;

    public MyStack(){
        this.elem = new int[DEFAULT_CAPACITY] ;
    }
    public void push(int val){
          if(isFull()){
              this.elem = Arrays.copyOf(this.elem , 2*this.elem.length);
              this.elem[this.usedSize++] = val ;
          }else{
              this.elem[this.usedSize++] = val ;
          }
    }
    public int pop(){
       if(empty()){
           throw new StackEmptyException("栈为空") ;
       }
       return this.elem[--this.usedSize] ;
    }
    public int peek(){
        if(empty()){
            throw new StackEmptyException("栈为空") ;
        }
        return this.elem[this.usedSize-1] ;
    }
    public int search(int key){
        for (int i = 0; i < this.usedSize; i++) {
            if(this.elem[i] == key){
                return i+1 ;
            }
        }
        return -1 ;
    }
    public boolean empty(){
      return this.usedSize == 0 ;
    }

     public boolean isFull(){
      return this.usedSize == this.elem.length ;
     }


}
